package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @RequestMapping("/input")
    public String strategy(Model model,String id){
        System.out.println(strategyCatalogService.selectAll());
        //判断是否有id，若有，则进行回显
        if(StringUtils.hasLength(id)){
            Strategy strategy = strategyService.selectOne(id);
            model.addAttribute("strategy", strategy);
        }
        //进行封装的时候需要新建一个类去封装里面的一些数据
        model.addAttribute("catalogs", strategyCatalogService.queryByDestAndCatalog());
        List<StrategyTheme> strategyThemes = strategyThemeService.selectAll();
        model.addAttribute("themes",strategyThemes);
        return "/strategy/input";
    }

    //列表显示
    @RequestMapping("/list")
    public String list(Model model, StrategyQueryObject qo){
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);
        model.addAttribute("qo", qo);
        return "/strategy/list";
    }

    //添加数据
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }

    //关联目的地
    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests() {
        List<Strategy> strategys = strategyService.selectAll();
        System.out.println(strategys);
        return strategys;

    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyService.delete(id);
        return JsonResult.success();
    }





}
